/*
* mixins跨页面共用方法
*/

export default {
  data () {
    return {
      finished: false,
      loading: false,
      isLoading: false,
      isRefresh: false,
      dataList: [],
      page: 1,
      limit: 10,
      apiType: 'global',
      params: {},
      paramsUrl: '',
      apiUrl: ''
    };
  },
  methods: {
    onRefresh () {
      setTimeout(() => {
        this.isRefresh = false;
        this.page = 1;
        this.loading = false;
        this.getList(this.apiUrl, true);
        this.isLoading = false;
      }, 1000);
    },
    // 上拉加载
    onLoad () {
      console.log('onLoad');
      // 异步更新数据
      setTimeout(() => {
        this.page++;
        this.isRefresh = false;
        this.getList(this.apiUrl, false);
      }, 1000);
    },
    confirmGoods (data) {
      this.orderInfo = data;
      this.isPopupShow = true;
    },
    confirmrRecv () {
      this.$toast.loading({ duration: 0 });
      this.$API.user.order({
        no: this.orderInfo.order_no
      }, 'recv').then(res => {
        if (!res.code) {
          this.$toast('收货成功');
          this.isPopupShow = false;
          this.$route.name === 'orderDetail' && this.fetchOrderDetail();
        } else {
          this.$toast(res.message);
        }
        this.$toast.clear();
      });
    },
    getList (type, isRefresh) {
      let that = this;
      // console.log('当前页码：' + that.page);
      // console.log('限制个数：' + that.limit);
      !that.isRefresh && that.$toast.loading({ duration: 0 });
      that.$API[that.apiType][type]({
        page: that.page,
        limit: that.limit,
        ...that.params
      }, that.paramsUrl).then(res => {
        // console.log('当前接口返回数据：');
        // console.log(res);
        // console.log('当前获得列表长度：' + res.data.length);
        if (res.data && res.data.length > 0) {
          if (isRefresh) { // 如果是刷新，则重新获取初始数据
            that.dataList = res.data;
          } else { // 否则在初始数据的基础上添加新的数据
            that.dataList = that.dataList.concat(res.data);
          }
        } else {
          that.finished = true;
          that.dataList = [];
        }
        // 加载状态结束
        that.loading = false;
        if (that.dataList.length === res.count) {
          that.finished = true;
        }
        !that.isRefresh && that.$toast.clear();
      });
    }
  }
};
